package com.fenxiangle.yueding.feature.mine.model;

import android.text.TextUtils;

import com.fenxiangle.yueding.entity.bo.FoucsBo;
import com.fenxiangle.yueding.common.httprequestbody.HttpRequestBody;
import com.fenxiangle.yueding.entity.bo.LoginPram;
import com.fenxiangle.yueding.entity.bo.UserCenterBo;
import com.fenxiangle.yueding.feature.login.contract.LoginContract;
import com.fenxiangle.yueding.feature.login.dependencies.login.DaggerLoginModelComponent;
import com.fenxiangle.yueding.feature.mine.contract.MineContract;
import com.fenxiangle.yueding.feature.mine.dependencies.mine.DaggerMineModelComponent;
import com.fenxiangle.yueding.framework.api.ApiLib;
import com.fenxiangle.yueding.framework.api.UserApi;
import com.suozhang.framework.component.http.RxDataProcessFactory;
import com.suozhang.framework.entity.bo.UserInfoBo;

import org.json.JSONObject;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import io.reactivex.Observable;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

public class MineModel implements MineContract.Model {

    @Inject
    UserApi api;


    @Inject
    public MineModel() {
        DaggerMineModelComponent.builder().apiComponent(ApiLib.apiComponent()).build().inject(this);
    }


    @Override
    public Observable<String> uploadUserHead(String headUrl) {
        MultipartBody.Part headFilePart = null;

        if (!TextUtils.isEmpty(headUrl)){
            File headFile = new File(headUrl);
            RequestBody requestFile = RequestBody.create(MediaType.parse("application/otcet-stream"), headFile);
            headFilePart = MultipartBody.Part.createFormData("userHead", headFile.getName(), requestFile);
        }
        return api.uploadUserHead(headFilePart).compose(RxDataProcessFactory.dataPrepAndIoToMainTransformer());
    }

    @Override
    public Observable<UserCenterBo> getUserData() {
        return api.getUserCenterData().compose(RxDataProcessFactory.dataPrepAndIoToMainTransformer());
    }

    @Override
    public Observable<UserCenterBo> getOtherUserData(FoucsBo foucsBo) {
        return api.getOtherUserCenterData(foucsBo).compose(RxDataProcessFactory.dataPrepAndIoToMainTransformer());
    }

    @Override
    public Observable<String> upload_user_video(File files, Map<String,String> string) {
        Map<String, RequestBody> params = new HashMap<>();

            // create RequestBody instance from file
//            RequestBody  requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file);
            params.put("videoCoverImg" + "\"; filename=\"" + files.getName(), RequestBody.create(MediaType.parse("multipart/form-data"), files));
            //一定要加("AttachmentKey\"; filename=\"" +，不然失败
//            params.put(imgStr.getKey(),requestFile);


        for (Map.Entry<String, String> imgStr : string.entrySet()) {
            // create RequestBody instance from file
            RequestBody requestFile = RequestBody.create(MediaType.parse("text/plain;charset=UTF-8"), imgStr.getValue());
            //一定要加("AttachmentKey\"; filename=\"" +，不然失败
            params.put(imgStr.getKey(), requestFile);
        }
        return api.upload_user_video(params).compose(RxDataProcessFactory.dataPrepAndIoToMainTransformer());
    }

    @Override
    public Observable<String> attentionFollow(FoucsBo homeFilterBo) {
        return api.attentionFollow(homeFilterBo).compose(RxDataProcessFactory.dataPrepAndIoToMainTransformer());
    }

    @Override
    public Observable<String> attentionUnFollow(FoucsBo homeFilterBo) {
        return api.attentionUnFollow(homeFilterBo).compose(RxDataProcessFactory.dataPrepAndIoToMainTransformer());
    }
}
